import request from "@/utils/request";
import qs from 'qs'
import {formatDate} from "element-ui";

//获取所有的爬虫源
export function getCrawlSource() {
  return request({
    url:'/crawl/admin/getCrawlSource',
    method:'get'
  })
}

//根据爬虫源id获取爬虫源信息以及爬虫规则
export function getCrawlAndRule(crawlId){
  return request({
    url:`/crawl/admin/getCrawlAndRule/${crawlId}`,
    method:'get'
  })
}

//获得默认的爬虫源
export function defaultCrawlAndRule(){
  return request({
    url:'/crawl/admin/defaultCrawlAndRule',
    method:'get'
  })
}

//获取从其他网站爬取到的小说列表
export function crawlNovelList(query){
  return request({
    url:'/crawl/task/crawlNovelList',
    method:'get',
    params:query
  })
}

//爬取该小说内容
export function crawlNovel(crawlBookId){
  return request({
    url:`/crawl/task/crawlNovel/${crawlBookId}`,
    method:'get',
  })
}

//修改爬虫源状态
export function changeCrawlSourceStatus(id,sourceStatus){
  const data ={id,sourceStatus}
  return request({
    url:'/crawl/admin/changeCrawlSourceStatus',
    method:'post',
    data:data
  })
}

//添加或修改爬虫源信息
export function updateOrAddCrawlRule(form){
  const data = {...form}
  data.crawlRule = JSON.parse(data.crawlRule)
  return request({
    url:'/crawl/rule/updateAndAddCrawlRule',
    method:"post",
    data:data
  })
}


//添加爬虫源信息
export function addCrawlSource(data){
  return request({
    url:'/crawl/admin/addCrawlSource',
    method:'put',
    data:data
  })
}

//删除爬虫源信息
export function deleteCrawlSource(ids){
  const query = {
    ids:ids
  }
  //console.log(qs.stringify({name:'xiaoming',password:'123123'}))
  return request({
    url:'/crawl/admin/deleteCrawlSource',
    method:'delete',
    params:query,
    paramsSerializer:function (params) {
      return qs.stringify(params,{ arrayFormat: "repeat" })
    }

  })
}

//修改爬虫信心
export function updateCrawlSource(data){
  return request({
    url:'/crawl/admin/updateCrawlSource',
    method:'post',
    data:data
  })
}

//根据小说id，获取爬虫源信息
export function crawlSourceByBookId(bookId){
  const query={
    bookId,
  }
  return request({
    url:'/crawl/admin/crawlSourceByBookId',
    method:'get',
    params: query
  })
}

//爬取所有的章节信息,并和数据库中的章节信息做对比
export function crawlIndexList(query){
  return request({
    url:'/crawl/task/crawlIndexList',
    method:'get',
    params:query
  })
}

//将指定章节添加到数据库中
export function addBookIndex(data){
  return request({
    url:'/crawl/task/addBookIndex',
    method:'put',
    data:data
  })
}

//从redis中查询出当前的关键字
export function getFilterChapterWord(){
  return request({
    url:'/crawl/task/filterWord',
    method:'get',
  })
}

//将指定的关键字添加到Redis中
export function addFilterWord(keyword){
  var formData = new FormData
  formData.append("keyword",keyword)
  return request({
    url:'/crawl/task/addFilterWord',
    method:'put',
    data:formData
  })
}

//保存信息到Redis中
export function saveFilterWord(body){
  const data=JSON.parse(body)
  return request({
    url:'/crawl/task/updateFilterWord',
    method:'post',
    data:data
  })
}
